﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace IBuy.Entity
{
    public enum LogDegree{System,User,SystemAndUser};
    public class Log : IBuy.Entity.ILog
    {
        private string message;
        private LogDegree degree;

        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        public LogDegree Degree
        {
            get { return degree; }
            set { degree = value; }
        }

        /// <summary>
        /// 将系统日志保存到日志文件
        /// </summary>
        /// <param name="log">Log对象</param>
        public void SaveToSystemLog(Log log)
        {
            try
            {
                XmlDocument document = new XmlDocument();
                String location = Configuration.GetSystemLogLocation();
                document.Load(location);
                XmlNode root = document.SelectSingleNode("Logs");

                XmlElement logs = document.CreateElement("Log");
                
                XmlElement degree = document.CreateElement("Degree");
                degree.InnerText = log.degree.ToString();
                logs.AppendChild(degree);

                XmlElement message = document.CreateElement("Message");
                message.InnerText = log.message;
                logs.AppendChild(message);

                root.AppendChild(logs);
                document.Save(location);
            }
            catch (Exception e)
            {
            }
        }
        public void SaveToUserLog(Log log)
        {
            try
            {
                XmlDocument document = new XmlDocument();
                String location = Configuration.GetUserLogLocation();
                document.Load(location);
                XmlNode root = document.SelectSingleNode("Logs");

                XmlElement logs = document.CreateElement("Log");

                XmlElement degree = document.CreateElement("Degree");
                degree.InnerText = log.degree.ToString();
                logs.AppendChild(degree);

                XmlElement message = document.CreateElement("Message");
                message.InnerText = log.message;
                logs.AppendChild(message);

                root.AppendChild(logs);
                document.Save(location);
            }
            catch (Exception e)
            {
            }
        }
        /// <summary>
        /// 查询系统日志
        /// </summary>
        /// <returns>List<Log>集合类型</returns>
        public List<Log> QuerySystemLog()
        {
            return QueryLog(LogDegree.System);
        }
        /// <summary>
        /// 查询用户日志
        /// </summary>
        /// <returns>List<Log>集合类型</returns>
        public List<Log> QueryUserLog()
        {
            return QueryLog(LogDegree.User);
        }
        /// <summary>
        /// 查询系统和用户日志
        /// </summary>
        /// <returns>List<Log>集合类型</returns>
        public List<Log> QuerySystemAndUserLog()
        {
            List<Log> logs = new List<Log>();
            logs = QueryLog(LogDegree.System);
            List<Log> userLogs = QueryLog(LogDegree.User);
            if (userLogs.Count > 0)
            {
                logs.AddRange(userLogs);
            }
            return logs;
        }
        
        private List<Log> QueryLog(LogDegree LogDegree)
        {
            List<Log> logs = new List<Log>();
            try
            {
                string location = Configuration.GetSystemLogLocation();
                switch (LogDegree)
                {
                    case LogDegree.System:
                        location = Configuration.GetSystemLogLocation();
                        break;
                    case LogDegree.User:
                        location = Configuration.GetUserLogLocation();
                        break;
                }
                XmlDocument document = new XmlDocument();
                document.Load(location);

                XmlNodeList list = document.SelectSingleNode("Logs").ChildNodes;
                foreach (XmlNode item in list)
                {
                    Log log = new Log();
                    log.Degree = (LogDegree)Enum.Parse(typeof(LogDegree), item.SelectSingleNode("Degree").InnerText);
                    log.Message = item.SelectSingleNode("Message").InnerText;
                    logs.Add(log);
                }
                
            }
            catch (Exception e)
            {
                logs = null;
            }
            return logs;
        }
    }
}
